from websocket import create_connection
from time import sleep

def handle_task(msg_dict):
    sleep(1)
    print('task: %s' % msg_dict)
    cmd = generate_command(msg_dict)
    output = execute_task(cmd)
    result = parse_result(output)
    return '{"rtt": 1.3, "mdev": 2.2}'

ws = create_connection("ws://localhost:8080/websocket")
while True:
    print("Waite for task...")
    msg =  ws.recv()
    print('receive message: %s' % msg)
    result = handle_task(msg)
    ws.send(result)
    print('Send back result: %s' % result)
    ws.close()
    ws = create_connection("ws://localhost:8080/websocket")
